/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.persistence.hibernate.criteria.criterion;

import java.util.Map;

import com.genia.toolbox.basics.bean.Delimiter;
import com.genia.toolbox.basics.manager.CollectionManager;
import com.genia.toolbox.persistence.criteria.criterion.Criterion;
import com.genia.toolbox.persistence.criteria.criterion.Junction;
import com.genia.toolbox.persistence.hibernate.bean.Queryable;
import com.genia.toolbox.persistence.hibernate.bean.impl.DefaultQueryable;
import com.genia.toolbox.persistence.hibernate.bean.impl.QueryableDisplayObject;
import com.genia.toolbox.persistence.hibernate.criteria.HibernateCriteria;

/**
 * The default hibernate implementation of <code>Criterion</code> representing
 * a restriction for a criteria. It defines a constraint in a query. It uses
 * join and a <code>Delimiter</code> to generate the HQL recursively.
 */
public class DefaultHibernateCriterion
    extends DefaultQueryable<Queryable>
    implements HibernateCriterion, Junction
{

  /**
   * If not <code>null</code>, the <code>Criterion</code> to emulate if
   * this <code>DefaultHibernateCriterion</code> has no sub-Criterion.
   */
  private transient HibernateCriterion defaultCriterion;



  /**
   * Constructor.
   * 
   * @param delimiter
   *          the <code>Delimiter</code> used to assemble the sub queries
   * @param subQueryables
   *          the sub-Queryables
   */
  public DefaultHibernateCriterion(final Delimiter delimiter, final Queryable... subQueryables)
  {
    this(null, delimiter, subQueryables);
  }



  /**
   * Constructor.
   * 
   * @param defaultCriterion
   *          if not <code>null</code>, the <code>Criterion</code> to
   *          emulate if this <code>DefaultHibernateCriterion</code> has no
   *          sub-Criterion
   * @param delimiter
   *          the <code>Delimiter</code> used to assemble the sub queries
   * @param subQueryables
   *          the sub-Queryables
   */
  public DefaultHibernateCriterion(final HibernateCriterion defaultCriterion, final Delimiter delimiter, final Queryable... subQueryables)
  {
    super(delimiter, subQueryables);
    this.defaultCriterion = defaultCriterion;
  }



  /**
   * add a <code>Criterion</code> to the list of sub-criterion of this
   * junction.
   * 
   * @param criterion
   *          the sub-criterion to add.
   */
  public void add(final Criterion criterion)
  {
    getSubQueryables().add((HibernateCriterion) criterion);
  }



  /**
   * append to stringBuilder the part of the query represented by this object.
   * 
   * @param canHaveParameter
   *          can this part of the query use named parameters?
   * @param collectionManager
   *          The <code>CollectionManager</code> that allow us to perform
   *          joins
   * @param criteria
   *          the <code>HibernateCriteria</code> the represents the all query
   * @param stringBuilder
   *          the <code>StringBuilder</code> to append to.
   * @param queryableDisplayObject
   *          the <code>DisplayObject</code> that allows to convert this
   *          <code>Queryable</code> to a <code>String</code>
   */
  @Override
  public void appendQuery(final boolean canHaveParameter, final CollectionManager collectionManager, final HibernateCriteria<?> criteria, final StringBuilder stringBuilder,
      final QueryableDisplayObject queryableDisplayObject)
  {
    if (!getSubQueryables().isEmpty() || defaultCriterion == null) {
      super.appendQuery(canHaveParameter, collectionManager, criteria, stringBuilder, queryableDisplayObject);
    }
    else {
      defaultCriterion.appendQuery(canHaveParameter, collectionManager, criteria, stringBuilder, queryableDisplayObject);
    }
  }



  /**
   * Creates and returns a deep copy of this object.
   * 
   * @return a deep copy of this object
   */
  @Override
  public HibernateCriterion clone()
  {
    final Queryable[] subCriterions = new Queryable[getSubQueryables().size()];
    int index = 0;
    for (final Queryable subQueryable : getSubQueryables()) {
      if (subQueryable instanceof HibernateCriterion) {
        subCriterions[index] = ((HibernateCriterion) subQueryable).clone();
      }
      else {
        subCriterions[index] = subQueryable;
      }
      index++;
    }
    return new DefaultHibernateCriterion(defaultCriterion == null ? null : defaultCriterion.clone(), getDelimiter(), subCriterions);
  }



  /**
   * Fill the parameters <code>Map</code> with the constants elements of this
   * query.
   * 
   * @param parameters
   *          the <code>Map</code> to be filled
   */
  @Override
  public void fillParameters(final Map<String, Object> parameters)
  {
    if (!getSubQueryables().isEmpty() || defaultCriterion == null) {
      super.fillParameters(parameters);
    }
    else {
      defaultCriterion.fillParameters(parameters);
    }
  }

}
